import unittest
from io import StringIO
from report.parser.FutuStockPositionParser import FutuStockPositionParser


# 假设 FutuStockPositionParser 和 FutuStockPosition 类在 your_module 模块中

class TestFutuStockPositionParser(unittest.TestCase):

    def setUp(self):
        self.parser = FutuStockPositionParser()

    def test_parse(self):
        # 模拟 CSV 文件内容
        csv_data = """代码,名称,持有数量@市价,市值,可用数量,未实现盈亏比例,今日盈亏,平均成本,总盈亏金额,未实现盈亏,已实现盈亏,持仓占比,今日成交额,今日买入@均价,今日卖出@均价,交易币种,市场类型,股东代码
HK0001,股票1,100@20.0,2000,100,+10.0%,100,18.0,200,200,0,10%,1000,10@20.0,20@21.0,HKD,港股市场,12345
HK0002,股票2,200@30.0,6000,200,-5.0%,-100,31.0,-200,-200,0,20%,2000,20@30.0,30@31.0,HKD,港股市场,67890
"""
        file = StringIO(csv_data)

        # 调用解析方法
        positions = self.parser.parse(file)

        # 验证解析结果
        self.assertEqual(len(positions), 2)

        # 验证第一个持仓信息
        position1 = positions[0]
        self.assertEqual(position1.code, "HK0001")
        self.assertEqual(position1.name, "股票1")
        self.assertEqual(position1.quantity, 100)
        self.assertEqual(position1.market_price, 20.0)
        self.assertEqual(position1.market_value, 2000)
        self.assertEqual(position1.available_quantity, 100)
        self.assertEqual(position1.unrealized_profit_ratio, 0.1)
        self.assertEqual(position1.today_profit, 100)
        self.assertEqual(position1.average_cost, 18.0)
        self.assertEqual(position1.total_profit, 200)
        self.assertEqual(position1.unrealized_profit, 200)
        self.assertEqual(position1.realized_profit, 0)
        self.assertEqual(position1.position_ratio, 0.1)
        self.assertEqual(position1.today_turnover, 1000)
        self.assertEqual(position1.today_buy_quantity, 10)
        self.assertEqual(position1.today_buy_average_price, 20.0)
        self.assertEqual(position1.today_sell_quantity, 20)
        self.assertEqual(position1.today_sell_average_price, 21.0)
        self.assertEqual(position1.currency, "HKD")
        self.assertEqual(position1.market, "港股市场")
        self.assertEqual(position1.shareholder_code, "12345")

        # 验证第二个持仓信息
        position2 = positions[1]
        self.assertEqual(position2.code, "HK0002")
        self.assertEqual(position2.name, "股票2")
        self.assertEqual(position2.quantity, 200)
        self.assertEqual(position2.market_price, 30.0)
        self.assertEqual(position2.market_value, 6000)
        self.assertEqual(position2.available_quantity, 200)
        self.assertEqual(position2.unrealized_profit_ratio, -0.05)
        self.assertEqual(position2.today_profit, -100)
        self.assertEqual(position2.average_cost, 31.0)
        self.assertEqual(position2.total_profit, -200)
        self.assertEqual(position2.unrealized_profit, -200)
        self.assertEqual(position2.realized_profit, 0)
        self.assertEqual(position2.position_ratio, 0.2)
        self.assertEqual(position2.today_turnover, 2000)
        self.assertEqual(position2.today_buy_quantity, 20)
        self.assertEqual(position2.today_buy_average_price, 30.0)
        self.assertEqual(position2.today_sell_quantity, 30)
        self.assertEqual(position2.today_sell_average_price, 31.0)
        self.assertEqual(position2.currency, "HKD")
        self.assertEqual(position2.market, "港股市场")
        self.assertEqual(position2.shareholder_code, "67890")


if __name__ == '__main__':
    unittest.main()